﻿using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace VIT.Provider
{
    public class Faq : Faq_Base
    {
        #region Variable
        string user = HttpContext.Current.Session["userName"].ToString();
        #endregion

        #region Constructor
        public Faq(int Id = -1,
                        string LanguageId = "",
                        int FaqGroupId = 0,
                        string Question = "",
                        string AskBy = "",
                        string AnswerBy = "",
                        string Answer = "",
                        string Email = "",
                        string Address = "",
                        int Orders = 50,
                        bool Published = true)
        {
            this.Id = Id;
            this.LanguageId = LanguageId;
            this.FaqGroupId = FaqGroupId;
            this.Question = Question;
            this.AnswerBy = AnswerBy;
            this.Answer = Answer;
            this.AskBy = AskBy;
            this.Email = Email;
            this.Address = Address;
            this.Orders = Orders;
            this.Published = Published;
        }
        #endregion

        #region List
        public DataTable GetAll(string SortColumn)
        {
            if (SortColumn == "") SortColumn = "Orders";
            string strQuery = "select Id, FaqGroupId, Question, AskBy, AnswerBy, Email, Address, Orders, Published from Faq where LanguageID=@LanguageID and Deleted=0 order by " + SortColumn;
            SqlParameter LanguageId = new SqlParameter("@LanguageID", this.LanguageId);
            return ExecuteDataset(false, strQuery, LanguageId).Tables[0];
        }

        public bool Delete(int delete, int id)
        {
            string strQuery = "update Faq set Deleted=@Deleted, ModifyDate=@Now, ModifyByUser=@User, DeleteByUser=@DeleteByUser where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", id);
            SqlParameter D = new SqlParameter("@Deleted", delete);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter DeleteByUser = new SqlParameter("@DeleteByUser", (delete==1)?user:"");
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            return ExecuteNonQuery(false, strQuery, Id, D, User, DeleteByUser, Now) > 0;
        }

        public bool Publish(int publish, int id)
        {            
            string strQuery = "update Faq set Published=@Published, ModifyDate=@Now, ModifyByUser=@User where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", id);
            SqlParameter P = new SqlParameter("@Published", publish);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            return ExecuteNonQuery(false, strQuery, Id, P, User, Now) > 0;
        }

        public bool Order(string order, int id)
        {
            string strQuery = "update Faq set Orders=Orders " + order + " 1, ModifyDate=@Now, ModifyByUser=@User where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", id);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            return ExecuteNonQuery(false, strQuery, Id, User, Now) > 0;
        }
        #endregion

        #region Edit
        public void GetSingle()
        {
            if (Exist())
            {
                string strQuery = "select FaqGroupId, Question, AskBy, Answer, AnswerBy, Email, Address, Orders, Published from Faq where Id=@Id and LanguageID=@LanguageId";
                SqlParameter Id = new SqlParameter("@Id", this.Id);
                SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
                DataTable dt = ExecuteDataset(false, strQuery, Id, LanguageId).Tables[0];

                if (dt.Rows.Count > 0)
                {
                    this.FaqGroupId = Convert.ToInt32(dt.Rows[0]["FaqGroupId"]);
                    this.Question = dt.Rows[0]["Question"].ToString();
                    this.AskBy = dt.Rows[0]["AskBy"].ToString();
                    this.Answer = dt.Rows[0]["Answer"].ToString();
                    this.AnswerBy = dt.Rows[0]["AnswerBy"].ToString();
                    this.Email = dt.Rows[0]["Email"].ToString();
                    this.Address = dt.Rows[0]["Address"].ToString();
                    if (!string.IsNullOrEmpty(dt.Rows[0]["Orders"].ToString()))
                        this.Orders = int.Parse(dt.Rows[0]["Orders"].ToString());
                    if (!string.IsNullOrEmpty(dt.Rows[0]["Published"].ToString()))
                        this.Published = (bool)dt.Rows[0]["Published"];
                }
            }
            else this.Id = -1;
        }

        public bool Exist()
        {
            string strQuery = "select Id from Faq where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            return ExecuteScalar(false, strQuery, Id) != null;
        }

        public int Save()
        {
            if (Exist())
            {
                Delete(0, this.Id);
                return Update();
            }
            else
            {
                this.Id = Insert();
                return this.Id;
            }
        }

        public int Insert()
        {
            string sql = "insert into Faq(LanguageId, FaqGroupId, Question, AskBy, Answer, AnswerBy, Email, Address, Orders, Published, Deleted, CreateDate, ModifyDate, CreateByUser, ModifyByUser) values (@LanguageId, @FaqGroupId, @Question, @AskBy, @Answer, @AnswerBy, @Email, @Address, @Order, @Published, 0, @Now, @Now, @User, @User)";

            SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
            SqlParameter FaqGroupId = new SqlParameter("@FaqGroupId", this.FaqGroupId);
            SqlParameter Question = new SqlParameter("@Question", this.Question);
            SqlParameter AskBy = new SqlParameter("@AskBy", this.AskBy);
            SqlParameter Answer = new SqlParameter("@Answer", this.Answer);
            SqlParameter AnswerBy = new SqlParameter("@AnswerBy", this.AnswerBy);
            SqlParameter Email = new SqlParameter("@Email", this.Email);
            SqlParameter Address = new SqlParameter("@Address", this.Address);
            SqlParameter Orders = new SqlParameter("@Order", this.Orders);
            SqlParameter Published = new SqlParameter("@Published", this.Published);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            return ExecuteNonQuery(false, sql, LanguageId, FaqGroupId, Question, AskBy, Answer, AnswerBy, Email, Address, Orders, Published, User, Now);
        }

        public int Update()
        {
            string sql = "update Faq set FaqGroupId=@FaqGroupId, Question=@Question, AskBy=@AskBy, Answer=@Answer, AnswerBy=@AnswerBy, Email=@Email, Address=@Address, Orders=@Orders, ModifyDate=@Now, ModifyByUser=@User where Id=@Id";
            SqlParameter FaqGroupId = new SqlParameter("@FaqGroupId", this.FaqGroupId);
            SqlParameter Question = new SqlParameter("@Question", this.Question);
            SqlParameter AskBy = new SqlParameter("@AskBy", this.AskBy);
            SqlParameter Answer = new SqlParameter("@Answer", this.Answer);
            SqlParameter AnswerBy = new SqlParameter("@AnswerBy", this.AnswerBy);
            SqlParameter Email = new SqlParameter("@Email", this.Email);
            SqlParameter Address = new SqlParameter("@Address", this.Address);
            SqlParameter Orders = new SqlParameter("@Orders", this.Orders);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            return ExecuteNonQuery(false, sql, FaqGroupId, Question, AskBy, Answer, AnswerBy, Email, Address, Orders, User, Id, Now);
        }
        #endregion

        #region Trash
        public DataTable GetRecycleBin(string SortColumn)
        {
            if (SortColumn == "") SortColumn = "Orders";  
            string strQuery = "select Id, LanguageId, Question, AskBy, Answer, AnswerBy, Email, Address, CreateDate, ModifyDate, CreateByUser, ModifyByUser, DeleteByUser from Faq where Deleted=1 order by " + SortColumn;
            return ExecuteDataset(false, strQuery).Tables[0];
        }

        public bool Cleared(string id)
        {
            string strQuery = "delete from Faq where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", id);
            return ExecuteNonQuery(false, strQuery, Id)>0;
        }
        #endregion
    }
}
